package com.java2007.hotel.service.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.DetailOrderDao;
import com.java2007.hotel.dao.FoodTableDao;
import com.java2007.hotel.dao.OrderDao;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.FoodTable;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.service.FoodTableService;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.utils.JdbcUtils;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 订单业务实体类
 * @author csl
 * @version V1.0
 * @date 2021/01/23
 **/
public class OrderServiceImpl  implements OrderService {
    private OrderDao orderDao = (OrderDao) BeanFactory.getBean(Constant.ORDER_DAO_IMPL);
    private FoodTableDao foodTableDao = (FoodTableDao) BeanFactory.getBean(Constant.FOOD_TABLE_DAO_IMPL);

    private DetailOrderDao detailOrderDao = (DetailOrderDao) BeanFactory.getBean(Constant.DETAIL_ORDER_DAO_IMPL);
    /**
     * 查询所有订单
     * @return
     */
    @Override
    public List<Order> selectAll() throws SQLException {
        List<Order> orders = orderDao.selectAll();
        for (int i = 0; i < orders.size(); i++) {
            FoodTable foodTable = foodTableDao.selectFoodTableById(orders.get(i).getTableId());
            if (foodTable != null){
                orders.get(i).setTableStr(foodTable.getName());
            }else {
                orders.get(i).setTableStr("已删除");
            }

        }
        return orders;
    }

    /**
     * 修改订单
     * @return
     */
    @Override
    public int updateOrderStatus(byte status, int id) throws SQLException {
        int i = orderDao.updateOrderStatus(status, id);
        return i;
    }

    /**
     * 增加一个订单
     * @param order
     * @return
     */
    @Override
    public int addOrder(Order order) throws SQLException {

        JdbcUtils.startTransaction();
        int i = 0;
        try {
            detailOrderDao.updateOrderNumber(order.getCreaterId(),order.getNumber());
            i = orderDao.addOrder(order);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        }finally {
            JdbcUtils.commit();
        }

        return i;
    }
    /**
     * 删除一个订单
     * @param id
     * @return
     */
    @Override
    public int delete(int id) throws SQLException {
        JdbcUtils.startTransaction();
        try {
            Order order = orderDao.selectById(id);
            if (order != null){
                detailOrderDao.deleteDetailOrderByNumber(order.getNumber());
                orderDao.delete(id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            throw  new RuntimeException("服务器错误！");
        }finally {
            JdbcUtils.commit();
        }

        return 1;
    }

    /**
     * 根据订单编号修改订单
     * @return
     */
    @Override
    public int updateOrderStatusByNumber(byte status, String number) throws SQLException {
        Order order = orderDao.selectByByNumber(number);
        Date date = new Date();
        JdbcUtils.startTransaction();
        try {
            foodTableDao.updateFoodTableStatus((byte) 1,order.getTableId(),date,date);
            orderDao.updateOrderStatusByNumber(status,number);
        } catch (SQLException e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
        }finally {
            JdbcUtils.commit();
        }

        return 0;
    }

    @Test
    public void test() throws SQLException {


        OrderServiceImpl orderService = new OrderServiceImpl();
        List<Order> orders = orderService.selectAll();
        for (Order order : orders) {
            System.out.println(order);
        }
    }
}
